﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BSCMFF.Core.DataAccessLayer;
using BSCMFF.Core.Exceptions.Database;

namespace BSCMFF.Web.WebServices
{
    /// <summary>
    /// Summary description for SaveDataFromQuestionnaire
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class SaveDataFromQuestionnaire : System.Web.Services.WebService
    {
        [WebMethod()]
        public void SaveData(String name)
        {
            string urldecoded = Server.UrlDecode(name);

            BSCEntities context = new BSCEntities();
            Survey_Form sf = new Survey_Form();

            //sf.Name = "novy";

            string survey_name, survey_description;
            int numberOfEmployees;

            if ((survey_name = HttpUtility.ParseQueryString(urldecoded).Get("Survey_Name")) != null)
            {
                sf.Name = survey_name;
            }
            if ((survey_description = HttpUtility.ParseQueryString(urldecoded).Get("Survey_Description")) != null)
            {
                sf.Description = survey_description;
            }

            if (HttpUtility.ParseQueryString(urldecoded).Get("NumberOfEmployees") != null)
            {
                numberOfEmployees = Int32.Parse(HttpUtility.ParseQueryString(urldecoded).Get("NumberOfEmployees"));
                sf.NumberOfEmployees = numberOfEmployees;
            }

            sf.CreationDate = DateTime.Today;
            

            SaveQuestions(sf, urldecoded, context);

            context.AddToSurvey_Form(sf);
            try
            {
                context.SaveChanges();
                
            }
            catch
            {
                throw new InconsistentDatabaseException("Survey couldn't be saved");
            }
        }

        private static void SaveQuestions(Survey_Form sf, string urldecoded, BSCEntities context)
        {
            int i = 0;
            string questionPrefix = "Question[Question][" + i + "]";
            string question;

            while ((question = HttpUtility.ParseQueryString(urldecoded).Get(questionPrefix + "[TextBoxQuestion]")) != null)
            {
                Survey_Question sq;
                if (question != "")
                {
                    sq = new Survey_Question();
                    sq.Question = question;
                    sq.Order = i;
                    SaveAnswers(urldecoded, questionPrefix, sq);
                    
                    sf.Survey_Question.Add(sq);
                                        
                }
                questionPrefix = "Question[Question][" + ++i + "]";
            }
        }

        private static void SaveAnswers(string urldecoded, string questionPrefix, Survey_Question sq)
        {
            string answer;
            int j = 0;
            string answerPrefix = "[Answer][" + j + "]";
            while ((answer = HttpUtility.ParseQueryString(urldecoded).Get(questionPrefix + answerPrefix + "[TextBoxAnswer]")) != null)
            {
                Survey_Answer sa;
                if (answer != "")
                {
                    sa = new Survey_Answer();
                    sa.Question_FK = sq.ID;
                    sa.Answer = answer;
                    sa.Order = j;
                    sq.Survey_Answer.Add(sa);
                }

                answerPrefix = "[Answer][" + ++j + "]";
            }
        }


        //[WebMethod()]
        //public string HelloToYou(string name)
        //{
        //    return "Hello " + name;
        //}
        //[WebMethod()]
        //public string sayHello()
        //{
        //    return "hello ";
        //}
    }
}
